package com.example.testproj.view;

import android.animation.ObjectAnimator;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testproj.R;

public class ViewTestActivity extends AppCompatActivity {
    private static final String TAG = "ViewTestActivity";
    private TextView mTv1, mTv2;
    private double lastx, lastY;
    private MyLinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_test);
        mTv1 = (TextView) findViewById(R.id.tv01);
        mTv1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                double x = event.getRawX();
                double y = event.getRawY();
                Log.d(TAG, "onTouch: " + event.getAction());
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    lastx = x;
                    lastY = y;
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    double dx = x - lastx;
                    double dy = y - lastY;
                    Log.d(TAG, "onTouch: dx==" + dx + ",dy==" + dy);
//            startAnimation(dx,dy);

                    //  moveMethod1(dx, dy);
                    moveMethod2(dx, dy);

                    lastx = x;
                    lastY = y;
                }
                return true;
            }
        });
        ViewGroup contentParent = (ViewGroup) findViewById(android.R.id.content);
        linearLayout = (MyLinearLayout) contentParent.getChildAt(0);


        mTv2 = (TextView) findViewById(R.id.tv02);
        mTv2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d(TAG, "onTouch: ACTION_DOWN");
//                        mTv2.getParent().requestDisallowInterceptTouchEvent(true);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d(TAG, "onTouch: ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d(TAG, "onTouch: ACTION_UP");
                        break;
                }
                return true;
            }
        });
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        double x=event.getRawX();
//        double y=event.getRawY();
//        Log.d(TAG, "onTouch: "+event.getAction());
//        if (event.getAction()==MotionEvent.ACTION_DOWN){
//            lastx=x;
//            lastY=y;
//        }else if (event.getAction()==MotionEvent.ACTION_MOVE){
//            double dx=x-lastx;
//            double dy=y-lastY;
//            Log.d(TAG, "onTouch: dx=="+dx+",dy=="+dy);
////            startAnimation(dx,dy);
//
//          //  moveMethod1(dx, dy);
//            moveMethod2(dx, dy);
//
//            lastx=x;
//            lastY=y;
//        }
//        return true;
//    }

    //根据属性动画的原理
    private void moveMethod2(double dx, double dy) {

        mTv1.setTranslationX((float) (mTv1.getTranslationX() + dx));
        mTv1.setTranslationY((float) (mTv1.getTranslationY() + dy));
    }

    //根据margin 原理
    private void moveMethod1(double dx, double dy) {
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) mTv1.getLayoutParams();
        marginLayoutParams.leftMargin += dx;
        marginLayoutParams.topMargin += dy;
        mTv1.setLayoutParams(marginLayoutParams);
    }

    private void startAnimation(double dx, double dy) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTv1, "translationX", (float) (mTv1.getTranslationX() + dx)).setDuration(3000);
        objectAnimator.start();

        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mTv1, "translationY", (float) (mTv1.getTranslationY() + dy)).setDuration(3000);
        objectAnimator2.start();
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_animation1:

                MyLinearLayout myLinearLayout = linearLayout;
                myLinearLayout.smoothScrollTo(1000, 0);
                break;
            case R.id.btn_animation2:

                handler.sendEmptyMessage(SCROLL_START);
                break;
        }
    }

    private static int DELAY_TIME = 20;
    private static int FRAME_COUNT = 50;
    private static int DEST = 1000;
    private int count = 0;
    private static final int SCROLL_TO = 0x99;
    private static final int SCROLL_START = 0x98;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int scrollX = 0;
            switch (msg.what) {
                case SCROLL_START:
                    count = 1;
                    scrollX = count * DEST / FRAME_COUNT;
                    linearLayout.scrollTo(scrollX, 0);
                    this.sendEmptyMessageDelayed(SCROLL_TO, DELAY_TIME);
                    break;
                case SCROLL_TO:
                    count++;
                    if (count <= FRAME_COUNT) {
                        scrollX = count * DEST / FRAME_COUNT;
                        linearLayout.scrollTo(scrollX, 0);
                        this.sendEmptyMessageDelayed(SCROLL_TO, DELAY_TIME);
                    }
                    break;
            }
        }
    };
}
